﻿using DAL;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult FormDemo()
        {
            return View();
        }

        public ActionResult ListDemo()
        {
            return View();
        }

        public ActionResult CascadeDemo()
        {
            return View();
        }

        public JsonResult GetUserList()
        {
            using (UserService userService = new UserService())
            {
                return Json(userService.GetUserList(), JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public ContentResult AddUser(UserEntity userEntity)
        {
            using (UserService userService = new UserService())
            {
                userEntity.Id = Guid.NewGuid().ToString();
                userService.AddUser(userEntity);
                return Content(userEntity.Id);
            }
        }

        [HttpPost]
        public ContentResult DeleteUser(string userId)
        {
            using (UserService userService = new UserService())
            {
                userService.DeleteUser(userId);
                return Content("ok");
            }
        }

        [HttpPost]
        public ContentResult UpdateUser(UserEntity userEntity)
        {
            using (UserService userService = new UserService())
            {
                userService.UpdateUser(userEntity);
                return Content("ok");
            }
        }

        public JsonResult GetAreaList(string parentId)
        {
            using (AreaService areaService = new AreaService())
            {
                var areaList = areaService.GetAreaList(parentId);
                return Json(areaList, JsonRequestBehavior.AllowGet);
            }
        }
    }
}